
package com.simple.modules.sys.controller;

import com.simple.common.utils.R;
import com.simple.modules.sys.entity.SysRoleEntity;
import com.simple.modules.sys.service.SysRoleMenuService;
import com.simple.modules.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色管理
 *
 *
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends AbstractController {
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;

	/**
	 * 角色列表
	 */
	@GetMapping("/getList")
	public R getList(@RequestParam(value = "roleName",required = false) String roleName){
		List<SysRoleEntity> resultList = sysRoleService.getListByRoleName(roleName);

		return R.ok().put("resultList", resultList);
	}

	
	/**
	 * 角色信息
	 */
	@GetMapping("/getInfoByRoleId")
	public R info(@RequestParam("roleId") Integer roleId){
		SysRoleEntity role = sysRoleService.getById(roleId);
		//查询角色对应的菜单
		List<Integer> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
		role.setMenuIdList(menuIdList);
		
		return R.ok().put("role", role);
	}
	
	/**
	 * 修改保存角色
	 */
//	@SysLog("保存/修改角色")
	@PostMapping("/saveOrUpdate")
	public R saveOrUpdate(@RequestBody SysRoleEntity role){
		role.setCreateUserId(getUserId());
		sysRoleService.saveOrUpdateRole(role);
		
		return R.ok();
	}

	
	/**
	 * 删除角色
	 */
//	@SysLog("删除角色")
	@DeleteMapping("/delete")
	public R delete(@RequestBody List<Integer> roleIdList){
		sysRoleService.deleteBatch(roleIdList);
		
		return R.ok();
	}
}
